package com.example.watertest;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.DrawMode;
import org.andengine.entity.primitive.Mesh;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.andengine.extension.physics.box2d.util.triangulation.EarClippingTriangulator;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.color.Color;
import org.andengine.util.adt.list.ListUtils;
import org.andengine.util.modifier.IModifier;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef;
import com.badlogic.gdx.physics.box2d.joints.LineJointDef;
import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef;

import android.hardware.SensorManager;
import android.util.Log;




public class Collisions extends BaseGameActivity implements IOnSceneTouchListener {

	 public static int cameraWidth = 800;
	  public static int cameraHeight = 800;
	  public Scene mScene;
	  public FixedStepPhysicsWorld mPhysicsWorld;
	  public Rectangle dynamicRect;
	  public Rectangle staticRect;
	  public Body dynamicBody;
	  public Body staticBody;
	  public boolean setFullAlphaForDynamicBody = false;
	  public boolean setHalfAlphaForDynamicBody = false;
	  public boolean setFullAlphaForStaticBody = false;
	  public boolean setHalfAlphaForStaticBody = false;
	  final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(2f,0f, 0.9f);
	  
	  public boolean isBodyContacted(Body pBody, Contact pContact)
	  {
		  if(pContact.getFixtureA().getBody().equals(pBody) || pContact.getFixtureB().getBody().equals(pBody))
			  return true;
		  return false;
	  }
	  
	  public boolean areBodiesContacted(Body pBody1, Body pBody2, Contact pContact)
	  {
			  if(pContact.getFixtureA().getBody().equals(pBody1) || pContact.getFixtureB().getBody().equals(pBody1))
			  if(pContact.getFixtureA().getBody().equals(pBody2) || pContact.getFixtureB().getBody().equals(pBody2))
				  return true;
			  return false;
	  }
	  
	  @Override
	  public Engine onCreateEngine(final EngineOptions
	      pEngineOptions) {
	    return new FixedStepEngine(pEngineOptions, 60);
	  }

	  @Override
	  public EngineOptions onCreateEngineOptions() {
	    EngineOptions engineOptions = new EngineOptions(true,
	      ScreenOrientation.LANDSCAPE_SENSOR, new
	        FillResolutionPolicy(), new Camera(0,
	          0, cameraWidth, cameraHeight));
	    engineOptions.getRenderOptions().setDithering(true);
	    engineOptions.getRenderOptions().
	      getConfigChooserOptions()
	        .setRequestedMultiSampling(true);
	    engineOptions.setWakeLockOptions(
	      WakeLockOptions.SCREEN_ON);
	    return engineOptions;
	  }

	  @Override
	  public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {
	    pOnCreateResourcesCallback.
	      onCreateResourcesFinished();
	  }

	  @Override
	  public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
	    mScene = new Scene();
	    mScene.setBackground(new Background(0.9f,0.9f,0.9f));
	    pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	
	  }
	  
	  
	  @Override
	  public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {
		  
		    mPhysicsWorld = new FixedStepPhysicsWorld(60, new
		    	      Vector2(0f,-SensorManager.GRAVITY_EARTH*2),
		    	        false, 8, 3);
	  		mScene.registerUpdateHandler(mPhysicsWorld);
	  		
	  		dynamicRect = new Rectangle(500f, 640f, 100f, 100f,
	  				this.getEngine().getVertexBufferObjectManager());
	  				dynamicRect.setColor(0f, 0.7f, 0f);
	  				dynamicRect.setAlpha(0.5f);
	  				mScene.attachChild(dynamicRect);
	  				dynamicBody = PhysicsFactory.createBoxBody(mPhysicsWorld,
	  				dynamicRect, BodyType.DynamicBody, boxFixtureDef);
	  				dynamicBody.setLinearDamping(0.4f);
	  				dynamicBody.setAngularDamping(0.6f);
	  				mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
	  				dynamicRect, dynamicBody));
	  				staticRect = new Rectangle(500f, 240f, 100f, 100f,
	  				this.getEngine().getVertexBufferObjectManager());
	  				staticRect.setColor(0f, 0f, 0.7f);
	  				staticRect.setAlpha(0.5f);
	  				mScene.attachChild(staticRect);
	  				staticBody = PhysicsFactory.createBoxBody(mPhysicsWorld,
	  				staticRect,
	  				BodyType.StaticBody, boxFixtureDef);
	  				
	  			
	  				
	  				mPhysicsWorld.setContactListener(new ContactListener(){
	  					@Override
	  					public void beginContact(Contact contact) {
	  					if(contact.isTouching())
	  					if(areBodiesContacted(staticBody,dynamicBody,contact))
	  					setFullAlphaForStaticBody = true;
	  					if(isBodyContacted(dynamicBody,contact))
	  					setFullAlphaForDynamicBody = true;
	  					}
	  					@Override
	  					public void endContact(Contact contact) {
	  					if(areBodiesContacted(staticBody,dynamicBody,contact))
	  					setHalfAlphaForStaticBody = true;
	  					if(isBodyContacted(dynamicBody,contact))
	  					setHalfAlphaForDynamicBody = true;
	  					}
	  					@Override
	  					public void preSolve(Contact contact, Manifold oldManifold) {}
	  					@Override
	  					public void postSolve(Contact contact, ContactImpulse impulse)
	  					{}
						
	  				});
	  				
	  				mScene.registerUpdateHandler(new IUpdateHandler() {
	  					@Override
	  					public void onUpdate(float pSecondsElapsed) {
	  					if(setFullAlphaForDynamicBody) {
	  					dynamicRect.setAlpha(1f);
	  					setFullAlphaForDynamicBody = false;
	  					} else if(setHalfAlphaForDynamicBody) {
	  					dynamicRect.setAlpha(0.5f);
	  					setHalfAlphaForDynamicBody = false;
	  					}
	  					if(setFullAlphaForStaticBody) {
	  					staticRect.setAlpha(1f);
	  					setFullAlphaForStaticBody = false;
	  					} else if(setHalfAlphaForStaticBody) {
	  					staticRect.setAlpha(0.5f);
	  					setHalfAlphaForStaticBody = false;
	  					}
	  					}
	  					@Override 
	  					public void reset() {}
	  				});
	  		
		  mScene.setOnSceneTouchListener(this);
		  pOnPopulateSceneCallback.onPopulateSceneFinished();
				
	  }

	  @Override
	  public boolean onSceneTouchEvent(Scene pScene, TouchEvent
	      pSceneTouchEvent) {
		 
	    return true;
	  }

	
		
	
	
}
